<?php
/**
 * This file is part of Task Agile.
 * Task Agile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * Task Agile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Task Agile.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   TaskAgile
 * @package    Controller
 * @copyright  Copyright (c) 20010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @license    http://www.gnu.org/licenses/gpl.txt GPLv3
 * @version    $Id$
*/
class Task_DashboardController extends Tev_Controller_Action
{
    public function indexAction()
    {
        $idUser = Zend_Auth::getInstance()->getIdentity()->id;

        $user = new User();
        $rowUser = $user->find($idUser)->current();
        $rowSetProjects = $rowUser->findProjectViaProjectUser();
        $projects = array();
        foreach ($rowSetProjects as $rowProject) {
            $projects[] = $rowProject->id;
        }

        // Tasks
        $task = new Task();

        if ($projects) {
            $selectNotMyTask        = $task->select()->where('fk_project in (?)', $projects)->where('fk_user <> ?', $idUser)->where('status = ?', Task::STATUS_ACTIVE)->order(array('fk_project', 'priority', 'name'));
            $this->view->notMyTasks = $task->fetchAll($selectNotMyTask);
        } else {
            $this->view->notMyTasks = array();
        }

        $this->view->myTasks = $task->fetchAll($task->select(true)->setIntegrityCheck(false)
                      ->join(array('p' => 'project'), 'p.id = task.fk_project', array('projectPriority' => 'priority'))
                      ->where('fk_user = ?', $idUser)
                      ->where('p.status = ?', Task::STATUS_ACTIVE)
                      ->where('task.status = ?', Task::STATUS_ACTIVE)
                      ->order(array('p.priority', 'fk_project', 'task.priority', 'name')));

        $this->view->myTasksFinished = $task->fetchAll(array('fk_user = ?' => $idUser, 'status = ?' => Task::STATUS_FINISHED), 'dt_finish desc');
        $this->view->myTasksInactive = $task->fetchAll(array('fk_user = ?' => $idUser, 'status = ?' => Task::STATUS_INACTIVE), 'name');
        $this->view->prioritys       = $task->getPrioritys();

        // Projects
        $project = new Project();
        $rowSetProjects = $project->fetchAll(null, 'name');
        unset($project);
        $projects = array();
        foreach ($rowSetProjects as $rowProject) {
            $projects[$rowProject->id] = $rowProject->name;
        }
        $this->view->projects = $projects;
        unset($projects);

        // Users
        $rowSetUsers = $user->fetchAll(null, 'name');
        unset($user);
        $users = array();
        foreach ($rowSetUsers as $rowUser) {
            $users[$rowUser->id] = $rowUser->name;
        }
        $this->view->users = $users;
        unset($users);
    }

    public function adminAction()
    {
    }
}
